<template>
  <div class="login-container">
    <svg-icon icon-class="lock" class="international-icon" @click="closeLock" />
    <el-dialog
      :close-on-press-escape="false"
      :visible.sync="centerDialogVisible"
      :show-close="false"
      fullscreen
      custom-class="lockbg"
      center>
      <el-form ref="lockForm" :model="lockForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">现金贷</h3>
        </div>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            v-model="lockForm.password"
            :placeholder="$t('login.password')"
            show-password
            auto-complete="on"
            @keyup.enter.native="handleLogina"
          />
          <!-- <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" />
          </span> -->
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogina">
            {{ $t('login.unlock') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getRSApassword } from '@/utils/auth'
import { unlock } from '@/api/admin'
export default {
  name: 'ScreenLock',
  data() {
    const validatePass = (rule, value = 0, callback) => {
      if (value.length < 5) {
        callback(new Error(this.$i18n.t('login.passwordError')))
      } else {
        callback()
      }
    }
    return {
      centerDialogVisible: false,
      loading: false,
      lockForm: {
        password: ''
      },
      loginRules: {
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      }
    }
  },
  mounted() {
    localStorage.getItem('lock') && (this.centerDialogVisible = true)
  },
  methods: {
    closeLock() {
      this.centerDialogVisible = true
      localStorage.setItem('lock', true)
    },
    handleLogina() {
      this.$refs.lockForm.validate(async valid => {
        if (valid) {
          this.loading = true
          const pub_key = localStorage.getItem('pub_key')
          const password = getRSApassword(pub_key, this.lockForm.password)
          try {
            const res = await unlock({ password })
            if (res.code !== 0) return false
            localStorage.removeItem('lock')
            this.$nextTick(() => {
              this.$refs['lockForm'].clearValidate()
              this.$refs['lockForm'].resetFields()
            })
            this.loading = false
            this.centerDialogVisible = false
          } catch (err) {
            this.loading = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style rel='stylesheet/scss' lang='scss'>
/* reset element-ui css */
$bg:#2d3a4b;
$light_gray:#eee;
.login-container {
  height: 32px;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
.international-icon {
  font-size: 20px;
  cursor: pointer;
  vertical-align: -5px!important;
}
.lockbg {
  background-color: #2d3a4b;
  color: #ffffff;
  .el-dialog__header {
    border-bottom: none;
  }
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container{
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
